package org.dubnation.base.excel.upload.controller;

import org.dubnation.base.enums.ResponseCode;
import org.dubnation.base.excel.upload.service.IUploadService;
import org.dubnation.base.excel.upload.util.HttpStatusEnum;
import org.dubnation.base.excel.upload.util.ResponseResult;
import org.dubnation.base.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RequestMapping("/upload")
@RestController
public class UploadController {

    @Autowired
    private IUploadService uploadService;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/employee")
    public ResponseResult<List<String>> upload(@RequestBody @Valid MultipartFile file) {
        Object result = uploadService.upload(file);
        if (result.toString().equals(HttpStatusEnum.BAD_REQUEST.getMessage()))
            return ResponseResult.fail("导入的excel为空");
        else if (((List<String>) result).size() == 0)
            return ResponseResult.success();
        else
            return ResponseResult.fail(ResponseCode.RESPONSE_CODE_4000020.getCode(),
                    ResponseCode.RESPONSE_CODE_4000020.getMessage(), (List<String>) result);
    }
}
